package com.vp.org.web.servlet;

import com.vp.org.dao.UserInfoDao;
import com.vp.org.dao.impl.UserInfoDaoImpl;
import com.vp.org.pojo.BookInfo;
import com.vp.org.pojo.UserInfo;
import com.vp.org.service.BookService;
import com.vp.org.service.UserService;
import com.vp.org.service.impl.BookServiceImpl;
import com.vp.org.service.impl.UserServiceImpl;
import com.vp.org.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@SuppressWarnings("RedundantThrows")
@WebServlet(name = "user", value = "/user")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    private BookService bookService = new BookServiceImpl();

    private UserInfo user;

    private String str;

    // 郭淑华 写
    private UserInfo userInfo;

    private UserInfoDao userInfoDao;

    // 用户注册
    @SuppressWarnings({"DuplicatedCode", "unused"})
    public String reg(HttpServletRequest req, HttpServletResponse resp) {
        initForm(req, resp);

        String path = "r:" + str + "register.jsp?ok=";
        if (userService.addUser(user) > 0) {
            return path + "1";
        } else {
            return path + "0";
        }
    }

    @SuppressWarnings("unused")
    void initForm(HttpServletRequest req, HttpServletResponse resp) {
        user = new UserInfo();

        try {
            BeanUtils.populate(user, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }

        str = req.getRequestURI() +"/";
    }

    // 用户登入
    @SuppressWarnings({"unused", "DuplicatedCode"})
    public String login(HttpServletRequest req, HttpServletResponse resp) {
        initForm(req, resp);

        user = userService.getUserInfo(user);

        if (user != null) {

            initImg(req, resp);
            req.getSession().setAttribute("user", user);

            return "r:" + str + "index.jsp";
        } else {
            return "r:" + str + "login.jsp?ok=0";
        }
    }

    @SuppressWarnings("unused")
    void initImg(HttpServletRequest req, HttpServletResponse resp) {
        BookInfo book = new BookInfo();

        HttpSession session = req.getSession();

        // 如果用户成功登入 先初始化图片信息
        session.setAttribute("hot", bookService.findByHot());
        book.setType("1");
        session.setAttribute("condition", bookService.findByCondition(book));
        book.setType("2");
        session.setAttribute("condition2", bookService.findByCondition(book));
        book.setType("3");
        session.setAttribute("condition3", bookService.findByCondition(book));
        book.setType("4");
        session.setAttribute("condition4", bookService.findByCondition(book));
        book.setType("5");
        session.setAttribute("condition5", bookService.findByCondition(book));
        
        session.setAttribute("discount", bookService.findByDiscount());
    }

    // 郭淑华 写
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String familyPhone = request.getParameter("familyPhone");
        String qq = request.getParameter("qq");
        String integral = request.getParameter("integral");

        userInfo = new UserInfo();
        userInfoDao = new UserInfoDaoImpl();

        userInfo.setPhone(phone);
        userInfo.setPassword(password);
        userInfo.setEmail(email);
        userInfo.setFamilyPhone(familyPhone);
        userInfo.setQq(qq);
        userInfo.setIntegral(Integer.parseInt(integral));

        if (userInfoDao.login(userInfo) > 0) {
            response.sendRedirect("/admin/userQuery.jsp");
        } else {
            // 添加失败
            response.sendRedirect("/admin/error.jsp");
        }
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        userInfo = new UserInfo();
        userInfoDao = new UserInfoDaoImpl();

        String phone = request.getParameter("phone");

        userInfo.setPhone(phone);


        if (userInfoDao.delete(userInfo) > 0) {
            // 删除成功
            response.sendRedirect("http://localhost:8080/user?method=findByPage&pageIndex7=1");
        } else {
            // 删除失败
            response.sendRedirect("/admin/error.jsp");
        }
    }

    public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收用户点击的上一页，下一页或者是指定的页数
        int pageIndex7 = Integer.parseInt(request.getParameter("pageIndex7"));

        userInfoDao = new UserInfoDaoImpl();

        // 分页查询（默认第一页）
        request.getSession().setAttribute("users",userInfoDao.selectUserInfo(pageIndex7));

        // 总页数
        request.getSession().setAttribute("userPage",userInfoDao.page());

        //存储当前这一页是第几页
        request.getSession().setAttribute("currentPage7",pageIndex7);

        // 地址重定向
        response.sendRedirect("admin/userQuery.jsp");

    }

    public void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();

        response.sendRedirect("/user/login.jsp");
    }
}